package com.servlet;

import com.pojo.Users;
import com.service.UserManagerService;
import com.service.impl.UserManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/*
* 处理用户管理请求
* */
@WebServlet("/userManager.do")
public class UserManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String flag=req.getParameter("flag");
        if ("addUser".equals(flag)){
            this.addUser(req,resp);
        }else if ("selectUser".equals(flag)){
            this.selectUser(req, resp);
        }else if ("preUpdate".equals(flag)){
            this.preUpdate(req,resp);
        }else if ("modifyUser".equals(flag)){
            this.modifyUser(req,resp);
        }else{
            this.dropUser(req,resp);
        }
    }

    //处理用户添加请求
    private void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Users users=this.createUsers(req);
        try {
            //调用service层代码
            UserManagerService userManagerService=new UserManagerServiceImpl();
            userManagerService.addUser(users);
            resp.sendRedirect("ok.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            resp.sendRedirect("error.jsp");
        }
    }

    /*
    * 处理查询用户请求
    * */
    private void selectUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        Users user=this.createUsers(req);
        try {
            UserManagerService userManagerService=new UserManagerServiceImpl();
            List<Users> list = userManagerService.selectUser(user);
            req.setAttribute("list",list);

            req.getRequestDispatcher("usermanager/viewUser.jsp").forward(req,resp);

        } catch (Exception e) {
            e.printStackTrace();
            resp.sendRedirect("error.jsp");
        }
    }

    /*
    * 预更新查询
    * */
    private void preUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String userid=req.getParameter("userid");
        try {
            UserManagerService userManagerService=new UserManagerServiceImpl();
            Users users = userManagerService.selectUserByUserId(Integer.parseInt(userid));

            //因为要放到页面中，因此需要先放入容器中
            req.setAttribute("user",users);
            req.getRequestDispatcher("usermanager/updateUser.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
            resp.sendRedirect("error.jsp");
        }
    }

    /*
    * 处理更新用户请求
    * */
    private void modifyUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String userid=req.getParameter("userid");
        Users users=this.createUsers(req);
        users.setUserid(Integer.parseInt(userid));

        try {
            UserManagerService userManagerService=new UserManagerServiceImpl();
            userManagerService.modifyUser(users);
            //DML操作用重定向
            resp.sendRedirect("ok.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            resp.sendRedirect("error.jsp");
        }
    }

    /*
    * 处理删除用户请求
    * */
    private void dropUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
       //取userid
        String userid=req.getParameter("userid");
        try {
            UserManagerService userManagerService=new UserManagerServiceImpl();
            userManagerService.dropUser(Integer.parseInt(userid));
            resp.sendRedirect("ok.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            resp.sendRedirect("error.jsp");
        }
    }


    private Users createUsers(HttpServletRequest req){
        Users users=new Users();
        String username = req.getParameter("username");
        String userpwd = req.getParameter("userpwd");
        String usersex = req.getParameter("usersex");
        String phonenumber = req.getParameter("phonenumber");
        String qqnumber = req.getParameter("qqnumber");
        users.setUsername(username);
        users.setUserpwd(userpwd);
        users.setUsersex(usersex);
        users.setPhonenumber(phonenumber);
        users.setQqnumber(qqnumber);
        return users;
    }
}
